package com.lehe.controller.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lehe.common.exceptions.LHAdminException;
import com.lehe.dao.AdminRoleDao;
import com.lehe.dao.XHUserListDao;
import com.lehe.entry.AdminRoleDto;
import com.lehe.entry.BaseResponse;
import com.lehe.entry.xh.UserDto;
import com.lehe.service.impl.XHAddUserService;

/**
 * 添加运营号，添加下级代理相关controller
 * @author Administrator
 *
 */
@RequestMapping("admin/addUser")
@Controller
public class XHAddUserController extends BaseAdminController{

	@Autowired
    private XHAddUserService xHAddUserService;
	
	@Autowired
    private XHUserListDao xHUserListDao;
	
	@Autowired
	private AdminRoleDao adminRoleDao;
	
	/**
	 * 运营号添加下属进入页面
	 * @return
	 */
	@RequestMapping("addSubUser")
	public String userList(Model model){
		model.addAttribute("showTopIdInput",1);
		Long roleId = this.getAdminUserRoleId();
        AdminRoleDto roleDto = adminRoleDao.queryById(roleId);
        if(roleDto.getName().equals("运营号")){
        	//运营号角色登陆的，添加下级时不允许输入运营号
        	model.addAttribute("showTopIdInput",0);
        }
        
		return "admin/xh_addUser/addSubUser";
	}
	
	/**
	 * 添加运营号界面
	 * @return
	 */
	@RequestMapping("addTopUser")
	public String addTopUser(Model model){
		
		return "admin/xh_addUser/addTopUser";
	}
	
	@RequestMapping("addSubUserSubmit")
    @ResponseBody
    public BaseResponse addSubUserSubmit(Model model, Integer utype, String parentId, String userId,String topId){
		
		System.out.println(utype+","+parentId+","+userId);
        
        Long roleId = this.getAdminUserRoleId();
        AdminRoleDto roleDto = adminRoleDao.queryById(roleId);
        Long topIdInt = null;
        if(roleDto.getName().equals("运营号")){
        	topIdInt = this.getAdminUserId();
        }else{
        	try {
				topIdInt = Long.valueOf(topId);
			} catch (NumberFormatException e) {
				BaseResponse baseResponse = new BaseResponse(BaseResponse.FAIL, "运营号不存在");
				return baseResponse;
			}
        }
        Long parentIdL = null;
        try {
        	if(parentId != null && !parentId.trim().equals(""))
        	parentIdL = Long.valueOf(parentId);
		} catch (NumberFormatException e) {
			BaseResponse baseResponse = new BaseResponse(BaseResponse.FAIL, "父级不存在");
			return baseResponse;
		}
        Long userIdL = null;
        try {
        	userIdL = Long.valueOf(userId);
		} catch (NumberFormatException e) {
			BaseResponse baseResponse = new BaseResponse(BaseResponse.FAIL, "用户不存在");
			return baseResponse;
		}
        
        BaseResponse baseResponse = xHAddUserService.addSubUser(parentIdL,userIdL,utype,topIdInt,this.getAdminUserId());
        return baseResponse;
    }
	
	@RequestMapping("addTopUserSubmit")
    @ResponseBody
    public BaseResponse addTopUserSubmit(Model model, String name, String account, String pwd, String phone,Integer persent){
		
		System.out.println(name+","+account+","+pwd+","+phone+","+persent);
        BaseResponse baseResponse = new BaseResponse(BaseResponse.FAIL, "保存失败");
        
        try {
        	baseResponse = xHAddUserService.addTopUser(name,account,pwd,phone,persent);
		} catch (LHAdminException e) {
			baseResponse.setMsg(e.getMessage());
		}
        
        return baseResponse;
    }
	
	@RequestMapping("queryUserInfo")
	public String queryUserInfo(Model m,@RequestParam String userId){
		
		Long i;
		try {
			i = Long.valueOf(userId);
		} catch (NumberFormatException e) {
			return "admin/xh_addUser/profile";
		}
		
		List<UserDto> result = xHUserListDao.queryUserList(null,null,i,null,null,null,null,null,null,null);
		if(result != null && !result.isEmpty()){
			UserDto dto = result.get(0);
			if(dto.getTopId() != null){
				m.addAttribute("msg","该玩家已经属于其他运营号:"+dto.getTopName()+"("+dto.getTopId()+")");
				return "admin/xh_addUser/profile";
			}
			m.addAttribute("userData",result.get(0));
		}
		return "admin/xh_addUser/profile";
	}
	
}
